#!/usr/bin/python
# 问题生成模型WebAPI入口，基于FastAPI实现
# 请使用http://47.105.158.15:8000/docs 测试接口情况
# 2022年12月9日

from fastapi import FastAPI, Form, File, UploadFile
import datetime
from model_service import *
from pdf_analysis import *

# 生成接口对象
app = FastAPI()

'''
提供2类接口：
第1类：有关接口信息的接口
第2类：模型服务接口
'''


# 第1类接口
# -------------------------------------------------------------------------------
@app.get("/")
def welcome_info():
    return {"message": "欢迎进入算法模块！"}


@app.get("/init")
def init_info():
    return init_result


# 第2类接口
# -------------------------------------------------------------------------------
@app.post("/ch_grammatical_error_correction")
def ch_grammatical_error_correction(question: str = Form(cgec_question)):
    return {"question": cgec_process(question)}


@app.post("/en_grammatical_error_correction")
def en_grammatical_error_correction(question: str = Form(egec_question)):
    return {"question": egec_process(question)}


# @app.post("/en_answer_question_generation")
# def en_answer_question_generation(text: str = Form(eaqg_text)):
#     return {"question": eaqg_process(text)}
#
# @app.post("/ch_answer_question_generation")
# def ch_answer_question_generation(text: str = Form(caqg_text)):
#     return {"question": caqg_process(text)}

@app.post("/ch_no_answer_question_generation")
def ch_no_answer_question_generation(text: str = Form(cnaqg_text)):
    return {"question": cnaqg_process(text)}


@app.post("/en_no_answer_question_generation")
def en_no_answer_question_generation(text: str = Form(enaqg_text)):
    return {"question": enaqg_process(text)}


@app.post("/en_distractor_generation")
def en_distractor_generation(context: str = Form(edg_context),
                             question: str = Form(edg_question),
                             answer: str = Form(edg_answer)):
    return {"distractor": edg_process(context, question, answer)}


@app.post("/ch_distractor_generation")
def ch_distractor_generation(answer: str = Form(cdg_answer)):
    return {"distractor": cdg_process(answer)}


@app.post("/en_question_answering")
def en_question_answering(context: str = Form(eqa_context),
        question: str = Form(eqa_question)):
    res = eqa_process(context, question)
    return {"answer": res[0],
            "answer_confidence": res[1]}


@app.post("/ch_question_answering")
def ch_question_answering(context: str = Form(cqa_context),
        question: str = Form(cqa_question)):
    res = cqa_process(context, question)
    return {"answer": res[0],
            "answer_confidence": res[1]}


@app.post("/ch_evaluation")
def ch_evaluation(context: str = Form(ch_eval_context),
          question: str = Form(ch_eval_question),
          reference: str = Form(ch_eval_ref), ):
    return ceval_process(context, question, reference)


@app.post("/en_evaluation")
def en_evaluation(context: str = Form(en_eval_context),
          question: str = Form(en_eval_question),
          reference: str = Form(en_eval_ref), ):
    return eeval_process(context, question, reference)


@app.post("/upload_file")
def upload_file(myfile: UploadFile = File(...)):
    contents = myfile.file.read()
    file_type = myfile.filename.split(".")[1]
    name = str(hash(str(datetime.datetime.now())))
    file_path = '../files/' + name + "." + file_type
    with open(file_path, 'wb') as f:
        f.write(contents)
    res = pdf_process(file_path)
    return {"results": res}


# @app.post("/en_named_entities_recognition")
# def en_named_entities_recognition(text: str = Form(en_ner_text)):
#     return ener_process(text)


@app.post("/ch_named_entities_recognition")
def ch_named_entities_recognition(text: str = Form(ch_ner_text)):
    return cner_process(text)
